<?php $this->load->view('templates/backend/header'); ?>
<?php $this->load->view('templates/backend/lib'); ?>
<h3><?php if ($this->uri->segment(4) == "add") echo $this->lang->line('create'); else echo $this->lang->line('edit') ?> <?php echo mb_strtolower($this->lang->line('listcustomers'), 'utf-8')?></h3>
<p class="required">Ghi chú : * Thông tin bắt buộc</p>
<style>
    .fone_num_cls{
        width:20%;
    }
    .btn-success,.btn-danger{
        vertical-align: top;
    }
</style>
<?php if (isset($flash_message)  && $flash_message!=NULL ): ?>
<div class="alert alert-success"><a class="close" listcustomers-dismiss="alert">x</a><strong><?php echo $flash_message; ?></strong></div>
<?php endif; ?>
<div class="well">
	<?php echo form_open();?>
            <div class="top-button">
                    <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary')); ?>
                    <a href="<?php echo site_url('auth/listcustomers'); ?>" class="btn"><?php echo $this->lang->line('cancel')?></a>
            </div>

            <div class="tab-content">	
			<div class="tab-pane active" id="main">
				<?php                                 
				//SDT
                                if ($this->uri->segment(4) == "add"){
                                    //action add
                                    echo '<div id="foneadd">';//begin id foneadd
                                    echo '<div id="fone_addon1" class="fonediv">';
                                    echo form_label($this->lang->line('phone').' <span class="required">*</span>', 'phone');
                                    echo form_input(array('name' => 'phone[]','id' => '1', 'value' => (isset($listcustomers->phone))?$listcustomers->phone:'','onblur'=>'existsphone(this)','class' => 'fone_num_cls','required'=>'required'));
                                    echo form_error('phone');
                                    echo ' ';
                                    echo form_button(array('adata' => 1,'content' => $this->lang->line('add'),'class' => 'btn btn-success'));
                                    echo '<span id="simple-msg-phone1"></span>';
                                    echo '</div></div>';//end id foneadd
                                } else {
                                    //action edit
                                    echo '<div id="foneadd">';//begin id foneadd
                                    foreach($listcustomers->phone as $key => $value){
                                        $count = $key + 1;
                                        echo '<div id="fone_addon'.$count.'" class="fonediv">';
                                        if($count == 1){
                                            echo form_label($this->lang->line('phone'), 'phone');
                                        }
                                        echo form_input(array('name' => 'phone[]','id' => $count, 'value' => $value,'onblur'=>'existsphone(this)','class' => 'fone_num_cls','required'=>'required'));
                                        echo form_error('phone');
                                        echo ' ';
                                        if($count == 1){
                                            echo form_button(array('adata' => $count,'content' => $this->lang->line('add'),'class' => 'btn btn-success'));
                                        } else {
                                            echo '<button name="" type="button" adata="'.$count.'" class="btn btn-danger" onclick="javascript:$(this).parent().remove();">Xóa</button>';
                                        }
                                        
                                        echo '<span id="simple-msg-phone'.$count.'"></span></div>';
                                        
                                        echo form_input(array('type'=>'hidden', 'name' => 'phone_fake[]' , 'value' => $value));
                                        
                                    }
                                    echo '</div>';//end id foneadd
                                }
                                
                                //ho ten
				echo form_label($this->lang->line('name').' <span class="required">*</span>', 'name');
				echo form_input(array('name' => 'name', 'id' => 'name', 'value' => (isset($listcustomers->name))?$listcustomers->name:'','required'=>'required'));
				echo form_error('name');
                                //Loai khach hang
                                echo form_label($this->lang->line('type_customers').' <span class="required">*</span>', 'type');
                                echo form_dropdown('type', $this->listcustomers_model->getListTypeCustomers(), (isset($listcustomers->type))?$listcustomers->type:'1','id="type"');
				echo form_error('type');
                                
                                //Địa chỉ
                                echo form_label($this->lang->line('address'), 'address');
				echo form_textarea(array('name' => 'address', 'id' => 'address', 'value' => (isset($listcustomers->address))?$listcustomers->address:'','class' => 'input-file','rows'=>'5'));
				echo form_error('address');
                                //Tỉnh thành                                
                                echo form_label($this->lang->line('province'), 'province');
				echo form_dropdown('province', $this->listcustomers_model->getListProvince(), (isset($listcustomers->province))?$listcustomers->province:'27','id="province"');
				echo form_error('province');
                                //Email
                                echo form_label($this->lang->line('email'), 'email');
				echo form_input(array('name' => 'email', 'id' => 'email', 'value' => (isset($listcustomers->email))?$listcustomers->email:''));
				echo form_error('email');
				//Yahoo
                                echo form_label($this->lang->line('yahoo'), 'yahoo');
				echo form_input(array('name' => 'yahoo', 'id' => 'yahoo', 'value' => (isset($listcustomers->yahoo))?$listcustomers->yahoo:''));
				echo form_error('yahoo');
                                //Skype
                                echo form_label($this->lang->line('skype'), 'skype');
				echo form_input(array('name' => 'skype', 'id' => 'skype', 'value' => (isset($listcustomers->skype))?$listcustomers->skype:''));
				echo form_error('skype');
                                //Facebook
                                echo form_label($this->lang->line('facebook'), 'facebook');
				echo form_input(array('name' => 'facebook', 'id' => 'facebook', 'value' => (isset($listcustomers->facebook))?$listcustomers->facebook:''));
				echo form_error('facebook');
                                //Birthday
                                echo form_label($this->lang->line('birthday'), 'birthday');
				echo form_input(array('name' => 'birthday', 'id' => 'birthday', 'value' => (isset($listcustomers->birthday))?$listcustomers->birthday:''));
				echo form_error('birthday');
                                //member
                                echo form_label($this->lang->line('air_member'), 'member');
                                echo form_input(array('name' => 'member', 'id' => 'member', 'value' => (isset($listcustomers->member))?$listcustomers->member:''));
				echo form_error('member');
                                //Thông tin hóa đơn
                                echo form_label($this->lang->line('info_bill'), 'info_bill');
				echo form_textarea(array('name' => 'info_bill', 'id' => 'info_bill', 'value' => (isset($listcustomers->info_bill))?$listcustomers->info_bill:'','rows'=>'5','maxlength'=>'1000'));
				echo form_error('info_bill');
                                //Ghi chú
                                echo form_label($this->lang->line('note'), 'note');
				echo form_textarea(array('name' => 'note', 'id' => 'note', 'value' => (isset($listcustomers->note))?$listcustomers->note:'','rows'=>'5'));
				echo form_error('note');
                                //salesID
                                echo form_input(array('type'=>'hidden', 'name' => 'salesID', 'id' => 'salesID', 'value' => (isset($listcustomers->salesID))?$listcustomers->salesID:$this->session->userdata['user_id']));
				?>
			</div>
		</div>
		<div class="bottom-button">
			<?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary')); ?>
			<a href="<?php echo site_url('auth/listcustomers'); ?>" class="btn"><?php echo $this->lang->line('cancel')?></a>
		</div>
	<?php form_close();?>
</div>

<?php $this->load->view('templates/backend/footer'); ?>

<script>
$(document).ready(function() {
    var fone_add = 2;
    $("button.btn-success").on('click',function (e) {
            $("#foneadd").append('<div id="fone_addon'+fone_add+'" class="fonediv" adata="'+fone_add+'"><input type="text" name="phone[]" value="" id="'+fone_add+'" onblur="existsphone(this)" class="fone_num_cls"> <button name="" type="button" adata="'+fone_add+'" class="btn btn-danger" onclick="javascript:$(this).parent().remove();">Xóa</button><span id="simple-msg-phone'+fone_add+'"></span></div>');
            fone_add = fone_add+1;
    });
    $('form').submit(function() {
            if($("#type").val() == null || $("#type").val() == 0){
                alert("Vui lòng chọn <?php echo $this->lang->line('type_customers')?>");
                return false;
            } else {
                $(this).find("button[type='submit']").prop('disabled',true);
            }
    });
});

function existsphone(input) {
        var sodt        = input.value;
        var tid         = input.id;
        var test        = /^[0-9]+$/.test(sodt);
        if(test == false){
            alert("<?php echo $this->lang->line('phone_is_invalid');?>");
            return false;
        }

        if(sodt.length < 10){
            alert("Số dt phải có ít nhất 10 số!");return false;
        } else {
            var postData    = $(this).serializeArray();
            var formURL     = '/auth/getphone/existsphone?term='+sodt;
            $("#simple-msg-phone"+tid).html("<img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/>");
            setTimeout(function () {
                $.ajax(
                        {
                                url : formURL,
                                type: "POST",
                                data : postData,
                                cache: true,
                                success:function(data) 
                                {
                                    if(data == 0){
                                        $("#simple-msg-phone"+tid).html("<img src='<?php echo base_url().'static/templates/backend/images/icon/error.png';?>'/> SĐT đã tồn tại trong danh bạ KH");
                                    } else {
                                        if(tid == 1){
                                            var data = JSON.parse(data);
                                            document.getElementById("name").value           = data['name'];
                                            document.getElementById("type").value           = data['type'];
                                            document.getElementById("address").value        = data['address'];
                                            document.getElementById("province").value       = data['province'];
                                            document.getElementById("email").value          = data['email'];
                                            document.getElementById("salesID").value        = data['salesID'];
                                        }
                                        $("#simple-msg-phone"+tid).html("<img src='<?php echo base_url().'static/templates/backend/images/icon/ok.png';?>'/> SĐT hợp lệ");
                                        $('button[type="submit"]').removeAttr('disabled');
                                    }
                                },
                                error: function() 
                                {
                                    $("#simple-msg").html('F5 thử lại!!!!!!');
                                }
                        });
            }, 1000);
        }
}

$(function() {
    var $birthday = $( "#birthday" );
        $birthday.datepicker({ 
            dateFormat: 'dd-mm-yy',
             changeMonth: true,
            changeYear: true,
            yearRange: "-100:+0"
        }).val();
});

$("#member").keyup(function() {
    $(this).val($(this).val().replace(/([a-z])/,function(s){return s.toUpperCase()}));
});
</script>